package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 千峰教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/admin/storage")
public class AdminStorageController {

    @Autowired
    DtsStorageService storageService;


    @RequestMapping("/create")
    public Object add(MultipartFile file) throws Exception {
        //截取后缀名  fdsklfjaslkfjlka.txtsdjflkasjdflksd.jpg
        String originalFilename = file.getOriginalFilename();//获取原始文件名
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));

        DtsStorage storage = storageService.insert(ext, (int) file.getSize(), file.getBytes());
        return ResponseUtil.ok(storage);
    }


    @GetMapping("/list")
    public Object list(
            @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(name = "limit", required = false, defaultValue = "20") Integer limit,
            String key,
            String name,
            @RequestParam(name = "sort", required = false, defaultValue = "add_time") String sort,
            @RequestParam(name = "order", required = false, defaultValue = "asc") String order
    ) {
        IPage<DtsStorage> ipage = storageService.queryPage(page, limit, key, name, sort, order);

        Map<String, Object> data = new HashMap<>();
        data.put("total", ipage.getTotal());
        data.put("items", ipage.getRecords());
        return ResponseUtil.ok(data);
    }
}
